<script>
import {defineComponent} from 'vue'

export default defineComponent({
  name: "showInfo",
  setup(props, context) {
    // 在子组件里修改父组件通过props传递过来的数据(这种修改数据不规范，Vue中遵循的是单向数据流，子组件尽量不修改父组件传递过来的props)
    const changeInfo1 = () => {
      props.info.name = "TAOPINQI"
    }

    //触发自定义事件让父组件去修改数据(规范)
    const changeInfo2 = () => {
      context.emit("changeInfoName", "TAOPINQI")
    }

    // 在子组件里修改父组件通过props传递过来的数据父组件使用了readonly函数包裹了此时子组件不能修改(报错)
    const readonlyInfo = () => {
      readonlyInfo.name = "ASDFGF"
    }
    return {
      changeInfo1,
      changeInfo2,
      readonlyInfo
    }
  },
  emits: ["changeInfoName"],
  props: {
    //父组件定义的info是reactive数据
    info: {
      type: Object,
      default: () => {
        return {}
      }
    },
    //父组件定义的info是readonly数据
    readonlyInfo: {
      type: Object,
      default: () => {
        return {}
      }
    }
  }
})
</script>

<template>
  <h2>showInfo:{{ info }}</h2>

  <h2>showInfo:{{ readonlyInfo }}</h2>
  <button @click="changeInfo1">在showInfo组件里面修改父组件通过props传递过来info</button>
  <button @click="changeInfo2">通过自定义事件通知父组件修改info</button>
  <button @click="readonlyInfo">在showInfo组件里面修改父组件通过props传递过来readonlyInfo</button>
</template>

<style scoped lang="less">

</style>
